
import java.io.*;
import java.net.*;
import java.util.*;

public class UDPClient extends Thread {
	
	private String serverIP;
	private int serverPort;
	public int clientPort;
	
	public String caleFisier, idFisier, numeFiltru;
	public int operatie, lungime, inaltime; // 1 - put, 2 - get, 3 - filter, 4 - resize
	
	public UDPClient(String serverIP, int serverPort, int clientPort) {
		this.serverIP = serverIP;
		this.serverPort = serverPort;
		this.clientPort = clientPort;
	}
	
	public void run() {
		try {
			DatagramSocket socket = new DatagramSocket(this.clientPort);
			
		    System.out.println("transmit packet info");
		    byte[] msg;
		    InetAddress serverAddr = InetAddress.getByName(this.serverIP);
		    switch(this.operatie){
				case 1:{ 
					//create file object
				    File file = new File(this.caleFisier);
				    byte [] fileContent = new byte[1];
				    try
				    {
				    	//create FileInputStream object
				    	FileInputStream fin = new FileInputStream(file);
				    	fileContent = new byte[(int)file.length()];
				     
				    	fin.read(fileContent);
				     
				    }
				    catch(FileNotFoundException e)
				    {
				      System.out.println("File not found" + e);
				    }
				    catch(IOException ioe)
				    {
				      System.out.println("Exception while reading the file " + ioe);
				    }
					msg = ("put " + this.caleFisier + " Start:" +  file.length()).getBytes();	
					DatagramPacket packet_info = new DatagramPacket(msg, 0, msg.length, serverAddr, this.serverPort);
					socket.send(packet_info);
					
					System.out.println("transmit packet data " + new String(msg));
					DatagramPacket packet_data = new DatagramPacket(fileContent, 0, (int)file.length(), serverAddr, this.serverPort);
					socket.send(packet_data);
				}
				break;
				case 2:{
					msg = ("get " + this.idFisier + " Start:").getBytes();
					DatagramPacket packet_info = new DatagramPacket(msg, 0, msg.length, serverAddr, this.serverPort);
					socket.send(packet_info);
				}
				break;
				case 3:{
					msg = ("filter " + this.numeFiltru + " " + this.idFisier + " Start:").getBytes();
					DatagramPacket packet_info = new DatagramPacket(msg, 0, msg.length, serverAddr, this.serverPort);
					socket.send(packet_info);
				}
				break;
				case 4:{
					msg = ("resize " + this.lungime + " " + this.inaltime + " " + this.idFisier + " Start:").getBytes();
					DatagramPacket packet_info = new DatagramPacket(msg, 0, msg.length, serverAddr, this.serverPort);
					socket.send(packet_info);
				}
				break;
				default:
					msg = ("comanda_necunoscuta" + " Start:").getBytes();
				break;			
			}
			
			System.out.println("primesc packet");	
			byte[] recvBuf = new byte[1024];
			DatagramPacket ansPacket = new DatagramPacket(recvBuf, 0, recvBuf.length);
			socket.receive(ansPacket);
			System.out.println("am primit packet");	
			//System.out.println(new String(ansPacket.getData(), 0, ansPacket.getLength()));
			switch(this.operatie){
				case 1: 
					System.out.println("ID = " + new String(ansPacket.getData(), 0, ansPacket.getLength()));
				break;
				case 2:
					FileOutputStream outfisier = new FileOutputStream(this.caleFisier); 
					
					String value = new String (ansPacket.getData());
					byte[] recvBuf2 = new byte[Integer.parseInt(value.trim())];
					DatagramPacket ansPacket2 = new DatagramPacket(recvBuf2, 0, recvBuf2.length);
					socket.receive(ansPacket2);
					try {  
					    outfisier.write(ansPacket2.getData());  
					} finally {  
					    outfisier.close();  
					}
				break;
				case 3:
					System.out.println("ID = " + new String(ansPacket.getData(), 0, ansPacket.getLength()));
				break;
				case 4:
					System.out.println("ID = " + new String(ansPacket.getData(), 0, ansPacket.getLength()));
				break;
				default:
					System.out.println(new String(ansPacket.getData(), 0, ansPacket.getLength()));
				break;		
			}
			// Wait a bit before sending the next message.
			Thread.sleep(100);
			socket.close();			
		} catch (Exception e) {
			System.err.println("TCPClient-" + this.hashCode() + " exception: " + e);
			e.printStackTrace();
		}
	}
}
